<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库类型" prop="type" v-if="pageType === 'stock'">
        <el-select v-model="queryParams.type" placeholder="请选择仓库类型" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['recovery:stock:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" border>
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="名称" align="center" prop="stockName">
        <template v-slot="scope">
          <router-link :to="'/recovery/stock/' + scope.row.stockId + '?name=' + scope.row.stockName" class="link-type">
            <span>{{scope.row.stockName}}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="容量" align="center" prop="capacity">
        <template v-slot="scope">
          <span>{{scope.row.capacity === -1 ? '不限': scope.row.capacity}}</span>
        </template>
      </el-table-column>
      <el-table-column label="已存数" align="center" prop="savesNum" />
      <el-table-column label="负责人" align="center" prop="userId" :formatter="userFormatter" />
      <el-table-column label="仓库类型" align="center" prop="stockType">
        <template v-slot="scope">
          <el-tag v-for="item in typeOptions" :type="item.listClass" v-if="scope.row.type === item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.hasSystem === 0"
            v-hasPermi="['recovery:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['recovery:stock:remove']"
            v-if="scope.row.hasSystem === 0 && scope.row.savesNum === 0"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <el-select filterable clearable v-model="form.userId">
            <el-option
              v-for="user in userList"
              :label="user.nickName"
              :value="user.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="仓库类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择仓库类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
              v-if="dict.dictValue !== 'default'"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库容量" prop="capacity">
          <el-input-number v-model="form.capacity" />
          <span style="font-size: 12px;color: #909399">-1代表不限量</span>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStockPower, getStock, delStock, addStock, updateStock, exportStock } from "@/api/recovery/stock";
import {listUser} from "@/api/system/user"

export default {
  name: "Stock",
  components: {
  },
  data() {
    return {
      //页面类型
      pageType : undefined,
      userList: [],
      // 遮罩层
      loading: true,
      // 仓库类型字典
      typeOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库管理表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockName: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId : [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        stockName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    let arr = this.$route.path.split("/");
    this.pageType = arr[1];
    if(this.pageType !== "stock") {
      this.queryParams.type = arr[1];
    }

    this.getList();
    this.getUserList();
    this.getDicts("recovery_stock_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 用户格式 */
    userFormatter(row) {
      for(let i = 0; i < this.userList.length; i++) {
        if(row.userId === this.userList[i].userId) {
          return this.userList[i].nickName;
        }
      }
    },
    /** 用户列表 */
    getUserList() {
      listUser({}).then(resp => {
        this.userList = resp.rows;
      })
    },
    /** 查询仓库管理列表 */
    getList() {
      this.loading = true;
      listStockPower(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockId: null,
        stockName: null,
        userId: null,
        remark: null,
        type: null,
        capacity: -1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockId = row.stockId || this.ids
      getStock(stockId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stockId != null) {
            updateStock(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stockIds = row.stockId || this.ids;
      this.$confirm('是否确认删除仓库管理编号为"' + stockIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStock(stockIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有仓库管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStock(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
